package com.hm.hihttp.slice;

import com.hm.hihttp.ResourceTable;
import com.hm.hihttp.util.HiCallBack;
import com.hm.hihttp.util.HiHttpUtil;
import com.hm.hihttp.util.HiResponse;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                text.setText("正在Get访问...,请稍等...");
                String url = "https://fanyi.baidu.com/";
                HiHttpUtil.get(url, new HiCallBack.HiCallBackString() {
                    @Override
                    public void onFailure(int code, String errorMessage) {
                        text.setText("Get访问出错：" + errorMessage);
                    }

                    @Override
                    public void onResponse(String response) {
                        text.setText("Get访问页面内容为：\n" + response);
                        text.setMultipleLine(true);
                    }
                });
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
